package com.sgl.gk.controller;

import com.sgl.common.annotation.Log;
import com.sgl.common.core.controller.BaseController;
import com.sgl.common.core.domain.AjaxResult;
import com.sgl.common.core.page.TableDataInfo;
import com.sgl.common.enums.BusinessType;
import com.sgl.common.utils.ServletUtils;
import com.sgl.common.utils.StringUtils;
import com.sgl.gk.domain.GkProductionLine;
import com.sgl.gk.service.GkProductionLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author:xec
 * @date:2022/10/31 18:50
 *
 * 产线管理的controller
 **/
@RestController
@RequestMapping("/gk/productionline")
public class GkProductionLineController extends BaseController {

    @Autowired
    GkProductionLineService gkProductionLineService;

    /**
     * 按条件分页查询产线信息
     * @param gkProductionLine 查询条件
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(GkProductionLine gkProductionLine){
        if(ServletUtils.getParameter("pageSize")!=null&&ServletUtils.getParameter("pageNum")!=null){
            startPage();
        }
        return getDataTable(gkProductionLineService.list(gkProductionLine));
    }

    // 产线名称调用链接，返回产线id、名称、相关工位id和名称
    @GetMapping("/allInfoAndStations")
    @ResponseBody
    public AjaxResult selectInfoById(){
        return gkProductionLineService.selectInfoByIds();
    }

    /**
     * 添加产线信息
     * @param gkProductionLine
     * @return
     */
    @Log(title = "产线",businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestBody GkProductionLine gkProductionLine){
        if (StringUtils.isEmpty(gkProductionLine.getName())||StringUtils.isEmpty(gkProductionLine.getCode())) {
            return AjaxResult.error("必需字段验证不通过");
        }
        if (StringUtils.length(gkProductionLine.getName())>50) {
            return AjaxResult.error("产线名称长度不能超过20个字符");
        }
        if (StringUtils.length(gkProductionLine.getCode())>20) {
            return AjaxResult.error("产线编码不能超过20个字符");
        }
        return gkProductionLineService.add(gkProductionLine);
    }

    /**
     * 修改产线信息
     * @param gkProductionLine
     * @return
     */
    @Log(title = "产线",businessType = BusinessType.UPDATE)
    @PutMapping("edit")
    @ResponseBody
    public AjaxResult edit(@RequestBody GkProductionLine  gkProductionLine){
        if (StringUtils.isEmpty(gkProductionLine.getName())||StringUtils.isEmpty(gkProductionLine.getCode())) {
            return AjaxResult.error("必需字段验证不通过");
        }
        if (StringUtils.length(gkProductionLine.getName())>50) {
            return AjaxResult.error("产线名称长度不能超过20个字符");
        }
        if (StringUtils.length(gkProductionLine.getCode())>20) {
            return AjaxResult.error("产线编码不能超过20个字符");
        }
        return gkProductionLineService.edit(gkProductionLine);
    }

    /**
     * 删除产线信息
     * @param ids
     * @return
     */
    @Log(title = "产线",businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ResponseBody
    public AjaxResult removeByIds(@PathVariable("ids") Long[] ids){
        return gkProductionLineService.removeByIds(ids);
    }
}
